package com.fgbf.todo.Activities;

import java.util.ArrayList;
import java.util.Calendar;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.fgbf.todo.R;
import com.fgbf.todo.MainClasses.Category;
import com.fgbf.todo.MainClasses.Task;
import com.fgbf.todo.Managers.TaskManager;

/**
 * GUI Class for the Task Screen
 * @author Warren Shenk
 * @date 2/20/2012
 *
 */
public class TaskActivity extends Activity {
	
	//////////////////////////////////////////////////////////////////////////////
	// INSTANCE DATA
	//////////////////////////////////////////////////////////////////////////////
	/**
	 * The task manager
	 */
	private TaskManager taskMgr;
	/**
	 * Buttons
	 */
	private Button newTaskBttn, mapBttn;
	/**
	 * EditTexts
	 */
	private EditText newTaskEtxt;
	/**
	 * TextViews
	 */
	private TextView currTasksTxt;
	/**
	 * TableLayouts
	 */
	private TableLayout taskTable;
	/**
	 * RadioButtons
	 */
	private RadioButton catPersonalRB, catSchoolRB, catWorkRB, catAllRB;
	/**
	 * ScrollView
	 */
	private ScrollView taskScroll;
	/**
	 * RadioGroups
	 */
	private RadioGroup catRG, compRG;
	/**
	 * DatePicker
	 */
	private DatePicker filterDatePicker;
	/**
	 * The date on the datepicker
	 */
	private long filterDate;
	/**
	 * CheckBox
	 */
	private CheckBox dateFilterCheckBox;
	/**
	 * boolean determining whether or not to filter by category
	 */
	private boolean filterByCategory;
	/**
	 * the category to be filtered by 
	 */
	private Category cat;
	/**
	 * boolean determining whether or not to filter by completion
	 */
	private boolean filterByCompletion;
	

	//////////////////////////////////////////////////////////////////////////////
	// METHODS
	//////////////////////////////////////////////////////////////////////////////
	/** 
     * Called when the activity is first created. 
     */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.task_screen);
		
		taskMgr = new TaskManager(TaskActivity.this);
		
		newTaskBttn = (Button) findViewById(R.id.bttn_new_task);
		newTaskBttn.setOnClickListener(createTaskListener);
		
		//mapBttn = (Button) findViewById(R.id.bttn_map);
		//mapBttn.setOnClickListener(mapBttnListener);
		
		taskTable = (TableLayout) findViewById(R.id.tbl_tasktable);
		
		currTasksTxt = (TextView) findViewById(R.id.txt_tasklist);
		
		catPersonalRB = (RadioButton) findViewById(R.id.radio_cat_personal);
		
		catSchoolRB = (RadioButton) findViewById(R.id.radio_cat_school);
		
		catWorkRB = (RadioButton) findViewById(R.id.radio_cat_work);
		
		catAllRB = (RadioButton) findViewById(R.id.radio_cat_all);
		
		catRG = (RadioGroup) findViewById(R.id.radiogroup_category);
		catRG.setOnCheckedChangeListener(categoryListener);
		
		compRG = (RadioGroup) findViewById(R.id.radiogroup_comp);
		compRG.setOnCheckedChangeListener(completionFilterListener);
		
		taskScroll = (ScrollView) findViewById(R.id.scrllvw_tasks);
		taskScroll.fullScroll(View.FOCUS_UP);
		
		dateFilterCheckBox = (CheckBox) findViewById(R.id.checkBox1);
		dateFilterCheckBox.setOnClickListener(checkBoxListener);
		
		filterDatePicker = (DatePicker) findViewById(R.id.datePicker1);
		Calendar c = Calendar.getInstance();
	    filterDatePicker.init(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), new filterDateListener());
		filterDate = c.getTimeInMillis();
		
		filterByCompletion = false;
		
		filterByCategory = false;
		cat = new Category("");
		
		this.generateTaskTable();
		
		
		
	}
	//--------------------------------------------------------------------------------
	
	/**
	 * Called when back button is pressed 
	 * Kills the instance of TaskActivity
	 */
	public void onBackPressed() {
		startActivity(new Intent(this.getApplicationContext(), LoginActivity.class));
		this.finish();
	}
	//---------------------------------------------------------------------------------
	
	/**
	 * Generates a table of Tasks on the TaskActivity
	 */
	private void generateTaskTable() {
		ArrayList<Task> taskList = taskMgr.getFilteredTasksFromDB(filterByCategory, cat, filterByCompletion, dateFilterCheckBox.isChecked(), filterDate);
		for (int i = 0; i < taskList.size(); i ++) {
			
			Task tempTask = taskList.get(i);
			Button tempButton = new Button(this.getApplicationContext());
			CheckBox tempCheckBox = new CheckBox(this.getApplicationContext());
			
			/*
			Resources res = this.getApplicationContext().getResources();
		    */
			
			tempCheckBox.setChecked(tempTask.isCompleted());
			tempCheckBox.setOnCheckedChangeListener(completedItemListener);
			
			tempButton.setText(tempTask.getName());
			tempButton.setOnClickListener(viewTaskListener);
			
			/*
			Drawable taskBttnDrawable = res.getDrawable(R.drawable.tsk_button_background);
			tempButton.setBackgroundDrawable(taskBttnDrawable);
			tempButton.setTextColor(Color.BLACK);
			*/

			TableRow.LayoutParams taskCellLp = new TableRow.LayoutParams(
			        ViewGroup.LayoutParams.FILL_PARENT,
			        ViewGroup.LayoutParams.FILL_PARENT,
			        1.0f);
			
			TableRow tempRow = new TableRow(this.getApplicationContext());
			tempRow.addView(tempCheckBox);
			tempRow.addView(tempButton, taskCellLp);
			tempRow.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

			
			taskTable.addView(tempRow);
		}
		
	}
	//---------------------------------------------------------------------------------

	
	/**
	 * Removes all views from the Task Table
	 */
	private void clearTaskTable() {
		taskTable.removeAllViews();
	}
	//--------------------------------------------------------------------------------

	
	//////////////////////////////////////////////////////////////////////////////
	// LISTENERS
	//////////////////////////////////////////////////////////////////////////////
	
	/**
	 * Listener, responds to view map button
	 */
	private OnClickListener mapBttnListener = new View.OnClickListener() {
		public void onClick(View v) {
			startActivity(new Intent(v.getContext(), LocationActivity.class));
		}
	};
	//---------------------------------------------------------------------------------

	/**
	 * Listener, starts a NewTaskAcivity on click
	 */
	private OnClickListener createTaskListener = new View.OnClickListener() {
		public void onClick(View v) {
			
			taskMgr.setIsTaskNew(true, TaskActivity.this);
			
			startActivity(new Intent(v.getContext(), ViewTaskActivity.class));
			TaskActivity.this.finish();
		}
	};
	//---------------------------------------------------------------------------------
	
	/**
	 * Listener, starts a ViewTaskActivity on click
	 */
	private OnClickListener viewTaskListener = new View.OnClickListener() {
		public void onClick(View v) {
			
			taskMgr.setIsTaskNew(false, TaskActivity.this);
			taskMgr.updateTaskToBeViewed(((Button)v).getText().toString(), TaskActivity.this);
			
			startActivity(new Intent(v.getContext(), ViewTaskActivity.class));
			TaskActivity.this.finish();
		}
	};
	//---------------------------------------------------------------------------------
	
	/**
	 * Listener, responds to a completed checkbox
	 */
	private CompoundButton.OnCheckedChangeListener completedItemListener = new CompoundButton.OnCheckedChangeListener() {
		
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			int buttonLocation = 1;
			
			TableRow tempRow = (TableRow) buttonView.getParent();
			Button tempButton = (Button) tempRow.getChildAt(buttonLocation);
			String taskName = tempButton.getText().toString();
			
			taskMgr.updateTaskCompletionInDB(taskName, isChecked);
			
			
			
			TaskActivity.this.clearTaskTable();
			
			
			TaskActivity.this.generateTaskTable();
			
		}
	};
	//---------------------------------------------------------------------------------
	
	/**
	 * Listener, responds to the category radio group
	 */
	private OnCheckedChangeListener categoryListener = new OnCheckedChangeListener() {

		public void onCheckedChanged(RadioGroup rb, int selectedRB) {
			TaskActivity.this.clearTaskTable();
			if (selectedRB == R.id.radio_cat_personal){
				cat.setCurrentCategory("Personal");
				filterByCategory = true;
			}
			else if (selectedRB == R.id.radio_cat_school){
				cat.setCurrentCategory("School");
				filterByCategory = true;
			}
			else if (selectedRB == R.id.radio_cat_work){
				cat.setCurrentCategory("Work");
				filterByCategory = true;
			}
			else {
				cat.setCurrentCategory("All");
				filterByCategory = false;
			}
			TaskActivity.this.generateTaskTable();
		}
		
	};
	//---------------------------------------------------------------------------------
	
	/**
	 * Listener, responds to filtering for incomplete items
	 */
	private OnCheckedChangeListener completionFilterListener = new OnCheckedChangeListener() {

		public void onCheckedChanged(RadioGroup arg0, int selectedRB) {
			TaskActivity.this.clearTaskTable();
			if(selectedRB == R.id.radio_comp_inc) {
				filterByCompletion = true;
			}
			else {
				filterByCompletion = false;
			}
			
			TaskActivity.this.generateTaskTable();
		}
		
	};
	//---------------------------------------------------------------------------------


	/**
	 * Listener, responds to the Due Date Picker
	 */
	private class filterDateListener implements OnDateChangedListener {

		public void onDateChanged(DatePicker arg0, int year, int month, int day) {
			Calendar c = Calendar.getInstance();
			c.set(year, month, day);
			filterDate = c.getTimeInMillis();
			
		}
	}
	//---------------------------------------------------------------------------------
	
	/**
	 * Listener
	 */
	private OnClickListener checkBoxListener = new View.OnClickListener() {
		public void onClick(View v) {
			TaskActivity.this.clearTaskTable();
			TaskActivity.this.generateTaskTable();
		}
	};

	
}
